package cn.itcast.ft.controller;

import cn.hutool.core.net.URLDecoder;
import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.itcast.config.VisibleThreadPoolTaskExecutor;
import cn.itcast.controller.IpBean;
import cn.itcast.ft.dto.FataDTO;
import cn.itcast.ft.entity.FTToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;

@RestController
@Slf4j
public class BMController {
    private static List<IpBean> list=new ArrayList<>();
    private static List<FTToken> tokenList=new ArrayList<>();
    @Autowired
    private VisibleThreadPoolTaskExecutor executor;
    @RequestMapping("/baiMaCreateMarket")
    public String baiMaCreateMarket(@RequestParam(value = "num",defaultValue = "10000") Integer num,
                               @RequestParam(value = "salePrice",defaultValue = "29") String salePrice,
                               @RequestParam(value = "goodsId",defaultValue = "24") String goodsId) {
        for(int i=0;i<num;i++){
            try {
//                ThreadUtil.execAsync(() -> {
//                    saveOrder(goodsId);
//                });
                executor.execute(()->{
                        saveOrder(goodsId);
                });
            } catch (Exception e) {
                log.error("#####异常####" + e.getMessage());
            }
        }
        executor.info();
        return null;
    }
//    public void saveOrder(String goodsId){
//        String result=null;
//        try{
//            JSONObject jsonObject=new JSONObject();
//            jsonObject.putOnce("result","");
//            jsonObject.putOnce("node_type","product");
//            jsonObject.putOnce("sku_id",goodsId);
//            jsonObject.putOnce("wallet","HF");
//            jsonObject.putOnce("url","pages/market/details?id=85&type=product&hidene=0&number=1");
//            result = HttpRequest.post("https://fata1.fataverse.com/asset/quickBuy")
//                    .body(jsonObject.toString())
//                    .header("Authorization", "9c7e9a5cabc924dc59c198769207358d")
//                    .execute().body();
//            FataDTO fataDTO=JSONUtil.toBean(result,FataDTO.class);
//            if(fataDTO!=null){
//                String decodedStr = URLDecoder.decode(fataDTO.getMessage(), Charset.forName("UTF-8"));
//                log.info("##返回结果##"+decodedStr+"###code#"+fataDTO.getCode());
//            }
//        }catch (Exception e){
//            log.error("##token数据异常###:"+e.getMessage()+"####返回数据：###"+result);
//        }
//    }
    public void saveOrder(String goodsId){
        String result=null;
        try{
            JSONObject jsonObject=new JSONObject();
            jsonObject.putOnce("tokenAuth",true);
            jsonObject.putOnce("pay_type",2);
            jsonObject.putOnce("id",goodsId);
            jsonObject.putOnce("password","");
            String json=jsonObject.toString();
            result = HttpRequest.post("https://bm.yiwu200.com/api/cang/buy")
                    .header("Authorization","Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JtLnlpd3UyMDAuY29tL2FwaS9hdXRoL2xvZ2luIiwiaWF0IjoxNzEzODM2NDczLCJleHAiOjE3MTY0Mjg0NzMsIm5iZiI6MTcxMzgzNjQ3MywianRpIjoiMWRHZmRWd0ZjalBXRVRqRSIsInN1YiI6IjMwNTIwNCIsInBydiI6IjIzYmQ1Yzg5NDlmNjAwYWRiMzllNzAxYzQwMDg3MmRiN2E1OTc2ZjcifQ.ha_5jZl5r4DjX24eDN-Du7QLGKI_mSXYoU6kdJsAXA4")
                    .body(jsonObject.toString())
                    .execute().body();
            log.info("##返回响应结果##"+result);
            FataDTO fataDTO=JSONUtil.toBean(result,FataDTO.class);
            if(fataDTO!= null && !StringUtils.isEmpty(fataDTO.getMessage())){
                String decodedStr = URLDecoder.decode(fataDTO.getMessage(), Charset.forName("UTF-8"));
                fataDTO.setMessage(fataDTO.getMessage());
            }
            if(fataDTO!=null && fataDTO.getStatus_code()==200){
                MailUtil.send("1027060531@qq.com", "收到订单", "您有一个订单，请赶紧支付！", false);
            }else{
                log.info("##返回结果##"+fataDTO.toString());
            }
        }catch (Exception e){
            log.error("##token数据异常###:"+e.getMessage()+"####返回数据：###"+result);
        }
    }
}
